package com.qf.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.blog.commons.utils.TimeAgoUtils;
import com.qf.blog.entity.Comment;
import com.qf.blog.dto.MyPage;
import com.qf.blog.mapper.CommentMapper;
import com.qf.blog.service.CommentService;
import com.qf.blog.vo.CommVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * User: key
 * ClassName:CommentServiceImpl
 * Date: 2023/6/2
 * Time: 22:35
 * description:
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    //    @Override
//    public MyPage<CommVo> commentPage(Integer id, MyPage page) {
//        MyPage<CommVo> myPage = commentMapper.commentPage(id, page);
//        for (CommVo c:
//             myPage.getRecords()) {
//            List<CommVo> replyList = commentMapper.getCommentVoList(c.getCommentEntity().getId(),2);
//            c.setReplyList(replyList);
//            System.out.println(replyList);
//        }
//        return myPage;
//    }
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public MyPage<CommVo> commentPage(Integer id, MyPage page) {
        return null;
    }

    // 1.先查询评论(评论人)
    @Override
    public List<CommVo> getCommentVoList(Integer id) throws ParseException {

        // 1.先查询评论(评论人)
        List<CommVo> commentVoList = baseMapper.getCommentVoList(id, 1);
        for (CommVo c : commentVoList) {
            String format = TimeAgoUtils.format(c.getCommentEntity().getCreateTime());
            c.getCommentEntity().setCreateTimeStr(format);
            System.out.println(format);

        }
        // 2.继续查询每个评论下面的回复
        for (CommVo commentVo : commentVoList) {

            List<CommVo> replyList = baseMapper.getCommentVoList(commentVo.getCommentEntity().getId(), 2);

            commentVo.setReplyList(replyList);
        }
        return commentVoList;
    }

    @Override
    public Long getCommentCount(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("entity_id",id);
        return baseMapper.selectCount(queryWrapper);
    }

}
